package com.foodlocker.maintenance;

import java.io.IOException;
import java.io.StringReader;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.JsonReader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.foodlocker.common.CommonFunction;
import com.foodlocker.common.Constant;
import com.foodlocker.machine.R;

public class NetworkConfigFragment extends Fragment {
	
	
	Button btn_confirm;
	TextView hint;
	EditText mid,msecret;
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
	 //return super.onCreateView(inflater, container, savedInstanceState);
		 View view =inflater.inflate(R.layout.fragment_networkconfig, container, false);
		btn_confirm= (Button) view.findViewById(R.id.btn_frg_network);
		hint= (TextView) view.findViewById(R.id.tvhint__frg_network);
		mid= (EditText) view.findViewById(R.id.etmid_frg_network);
		msecret=(EditText) view.findViewById(R.id.etmsecret_frg_network);
		
		btn_confirm.setOnClickListener(new View.OnClickListener() {
		
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String params[]= new String[10];
				JSONObject jsonData=new JSONObject();
				try {
					jsonData.put("MID", mid.getText().toString());
					jsonData.put("Msecret", msecret.getText().toString());
				} catch (JSONException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
	             
				params[0]=Constant.PostURLofMachineServlet; //PostCode
				params[1]="9";  
				params[2]=jsonData.toString();
			//	params[7]="1";
			//	params[8]="123456";
				
				new HttpPostTaskforCheck().execute(params);
				
			}
		});
		return view;
	}
	
	private  class HttpPostTaskforCheck extends android.os.AsyncTask<String,String,String>{

			

			@Override
			protected String doInBackground(String... params) {
				// TODO Auto-generated method stub
				  String result=	CommonFunction.UniversalHttpPost(params);			             
				return result;
			}
			
			@Override
			protected void onPostExecute(String result) {
				// TODO Auto-generated method stub
			  //	super.onPostExecute(result);
				Boolean ActionStatus=false;
				String ErrorInfo="";
			    
				Toast.makeText( NetworkConfigFragment.this.getActivity(), "服务器返回数据为"+result, Toast.LENGTH_SHORT).show();
				 JsonReader jsonReader= new JsonReader(new StringReader(result));
			        try {
						jsonReader.beginObject();   
						while(jsonReader.hasNext()){
							
								 String tagName=jsonReader.nextName();
								 if(tagName.equals("ActionStatus")) { 
									 ActionStatus=jsonReader.nextBoolean();
									 System.out.println(" ActionStatus------>"+String.valueOf( ActionStatus)); }
								 if(tagName.equals("ErrorInfo")) { 
									 ErrorInfo=jsonReader.nextString();
									 System.out.println(" ErrorInfo------>"+ErrorInfo); }
								
						}
			        }catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			        
			        hint.setText(ErrorInfo+ActionStatus);
			        if(ActionStatus){
			        	SharedPreferences sharedPreferences = NetworkConfigFragment.this.getActivity().getSharedPreferences("config", Context.MODE_PRIVATE);    	
			        	 Editor editor = sharedPreferences.edit();    
			        	 editor.putString("MID", mid.getText().toString());
			        	 editor.putString("Msecret", msecret.getText().toString());
			        	 editor.commit();
			        }
			}
	 }
	
	
	
	
	
	
	
}
